package com.ve.s2g.chain

import com.ve.s2g.getLogger
import com.ve.s2g.pojo.Project
import com.ve.s2g.util.*
import java.nio.file.Paths

/**
 * 自动处理冲突
 */
class AutoResolveConflictsHandle: AbstractHandler(){
    var log = getLogger<AutoResolveConflictsHandle>();
    override fun deal(dealObject: Any?) {

        when(dealObject){
            is List<*> -> {
                dealObject.forEach { it: Any? ->
                    autoResolveConflicts(it as Project)
                }
            }
            is Project ->{
                autoResolveConflicts(dealObject)
            }
            null ->{
                return
            }
            else -> {
                return
            }
        }
    }

    /**
     *
     */
    fun autoResolveConflicts(project:Project){
        var targetBranch = getString4Cfg("git_target_branch")
        var sourceBranch = getString4Cfg("git_source_branch")
        updateGitCode(project,sourceBranch,targetBranch)
        //resolveConflicats(project,sourceBranch,targetBranch)
    }
    /**
     * merge
     */
    private fun resolveConflicats(project: Project, sourceBranch: String, targetBranch: String) {
        val tempFilePath = "${Constants.LOCAL_GIT_ROOT_PATH}\\${project.name}\temp.txt"
        Paths.get(tempFilePath).readLines().filter { linecode: String ->
            ConflicatsType.matches(linecode)
        }.associate { linecode: String ->
            val value = ConflicatsType.matchesType(linecode)
            val f = value.getfile(linecode)
            f to value
        }.forEach{
            when(it.value){
                ConflicatsType.BOTH_ADDED -> {
                    Shell.execute("cmd.exe /c ${Constants.LOCAL_GIT_ROOT_PATH}\\${project.name}\\git checkout ${it.key} --ours")
                }
                ConflicatsType.BOTH_MODIFIED -> {

                }
                ConflicatsType.ADDED_BY_US -> {

                }
                ConflicatsType.ADDED_BY_THEM -> {

                }
                ConflicatsType.DELETED_BY_US -> {

                }
                ConflicatsType.DELETED_BY_THEM -> {

                }
            }
        }
    }

    /**
     * 更新指定branch的代码到本地目录
     */
    private fun updateGitCode(project: Project, sourceBranchName:String ,targetbranchName: String) {
        val gitCodePath = "${Constants.LOCAL_GIT_ROOT_PATH}\\${project.name}"
        val path = Paths.get(gitCodePath)
        if(path.notExists()){
            path.createDirectories()
        }
        Shell.execute("${getPathInCP("cmd/checkout_git_branch.bat")} ${project.gitFullPath()} $gitCodePath $sourceBranchName $targetbranchName")
    }

}
enum class ConflicatsType(val matchText:String){
    BOTH_ADDED("both added:"),
    BOTH_MODIFIED("both modified:"),
    ADDED_BY_US("added by us:"),
    ADDED_BY_THEM("added by them:") ,
    DELETED_BY_US("deleted by us:") ,
    DELETED_BY_THEM("deleted by them:"),
    NO("-=-=-=-=-");
    fun getfile(text: String):String?{
        return "\\s*${matchText}\\s*(.*)".toRegex().find(text)?.groupValues?.get(1)
    }
    companion object {
        fun matches(text:String):Boolean{
            for (type in ConflicatsType.values()){
                if(text.matches("\\s*${type.matchText}.*".toRegex())){
                    return true;
                }
            }
            return false;
        }
        fun matchesType(text: String): ConflicatsType {
            for (type in ConflicatsType.values()) {
                if (text.matches("\\s*${type.matchText}.*".toRegex())) {
                    return type;
                }
            }
            return NO;
        }
    }
}

fun main(args: Array<String>) {
    Shell.execute("${getPathInCP("cmd/checkout_git_branch.bat")} http://172.17.160.32:8082/BMC-Team/riil_bmc_adapter G:\\git_riil\\riil_bmc_adapter2 6.8.7_Hotfix dev")
//    val tempFilePath = "G:\\git_riil\\riil_bmc_adapter2\\temp.txt"
//    Paths.get(tempFilePath).readLines().filter { linecode: String ->
//        ConflicatsType.matches(linecode)
//    }.associate { linecode: String ->
//        val value = ConflicatsType.matchesType(linecode)
//        val f = value.getfile(linecode)
//        f to value
//    }.forEach{
//        println("G:\\git_riil\\riil_bmc_adapter2\\${it.key!!.replace("/","\\")}")
//    }
//    val text ="\tboth added:      adapter/src/main/webapp/WEB-INF/adapter-servlet.xml"
//    println("\\s*both added:\\s*(.*)".toRegex().find(text)?.groupValues?.get(1))

}
